package com.njitt.bysj.controller;

import com.njitt.bysj.base.BaseController;
import com.njitt.bysj.base.ResultInfo;
import com.njitt.bysj.bean.EmpBean;
import com.njitt.bysj.model.EmpModel;
import com.njitt.bysj.query.EmpQuery;
import com.njitt.bysj.srevice.EmpService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.concurrent.SubjectAwareScheduledExecutorService;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/3/19 20:31
 */
@Controller
@RequestMapping("emp")
public class EmpController extends BaseController {
    @Resource
    private EmpService empService;

    @RequestMapping("toEmpPage")
    public String toEmpPage() {
        return "emp/emp";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> selectEmpByQuery(EmpQuery empQuery) {
        return empService.selectEmpByQuery(empQuery);
    }


    @RequestMapping("toAddEmpPage")
    public String toAddEmpPage() {
        return "emp/addEmp";
    }

    @RequestMapping("addEmp")
    @ResponseBody
    public ResultInfo addEmp(EmpBean emp) {
        empService.addEmp(emp);
        return success("success");
    }


    @RequestMapping("toUpdateEmpPage")
    public String UpdateEmpPage(Integer id, Model model) {
        EmpBean empBean = empService.selectByPrimaryKey(id);
        model.addAttribute("emp", empBean);
        return "emp/updateEmp";
    }

    @RequestMapping("updateEmp")
    @ResponseBody
    public ResultInfo updateEmpRoleByEmpId(EmpBean empBean) {
        empService.updateEmpByEmp(empBean);
        return success("添加成功");
    }

    @RequestMapping("selectDesignerEmpByRoleInRoleName")
    @ResponseBody
    public List<Map<String, Object>> selectDesignerEmpByRoleInRoleName(String roleName) {
        return empService.selectDesignerEmpByRoleInRoleName(roleName);
    }

    @RequestMapping("toPasswordPage")
    public String toPasswordPage() {
        return "emp/updatePassword";
    }

    @RequestMapping("password")
    @ResponseBody
    public ResultInfo updatePassword(Integer empId, String oldPassword, String newPassword, String againPassword) {
        empService.updatePassword(empId, oldPassword, newPassword, againPassword);
        return success();
    }

    @RequestMapping("toModule")
    public String toModule() {
        return "emp/module";
    }

    @RequestMapping("toUser")
    public String toUser(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        EmpModel empModel = (EmpModel) subject.getPrincipal();
        System.out.println(empModel);
        request.setAttribute("emp", empModel);
        return "emp/user";
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultInfo update(EmpBean empBean) {
        empService.updateByPrimaryKeySelective(empBean);
        return success("success");
    }
}



